repo: Add `require_rev` method
authorColin Walters <walters@verbum.org>
Fri, 22 Oct 2021 13:38:09 +0000 (09:38 -0400)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:57 +0000 (12:53 -0400)
The `resolve_rev` C method should really have been
`resolve_rev_optional` from the start - it is more obviously wrong
in Rust because the input parameter `allows_noent` controls
whether the returned `Option` can ever be `None`.

I debated adding this to the C bindings, and may still do so,
but eh it's faster to write + ship in Rust, and the future of ostree is
Rust anyways.

rust-bindings/rust/src/repo.rs
rust-bindings/rust/tests/repo/mod.rs

index 432b3ee485997bc1fcbb22507a958de345f5aeff..827dcb4f4817f792fd9e5ca18800dc1864e8452a 100644 (file)
@@ -175,6 +175,13 @@ impl Repo {
         }
     }
 
+    /// Resolve a refspec to a commit SHA256.
+    /// Returns an error if the refspec does not exist.
+    pub fn require_rev(&self, refspec: &str) -> Result<glib::GString, Error> {
+        // SAFETY: Since we said `false` for "allow_noent", this function must return a value
+        Ok(self.resolve_rev(refspec, false)?.unwrap())
+    }
+
     /// Write a content object from provided input.
     pub fn write_content<P: IsA<gio::InputStream>, Q: IsA<gio::Cancellable>>(
         &self,
index 6f3100aa6dc54e59de6bcfff76861504abb4f0f3..f56e390e89a575bd56b4a22679d3b8b3b8d8d333 100644 (file)
@@ -10,9 +10,13 @@ mod checkout_at;
 fn should_commit_content_to_repo_and_list_refs_again() {
     let test_repo = TestRepo::new();
 
+    assert!(test_repo.repo.require_rev("nosuchrev").is_err());
+
     let mtree = create_mtree(&test_repo.repo);
     let checksum = commit(&test_repo.repo, &mtree, "test");
 
+    assert_eq!(test_repo.repo.require_rev("test").unwrap(), checksum);
+
     let repo = ostree::Repo::new_for_path(test_repo.dir.path());
     repo.open(NONE_CANCELLABLE).expect("OSTree test_repo");
     let refs = repo